//
//  DailyTableViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "DailyTableViewCell.h"

@interface DailyTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *DailyArr;


@end

@implementation DailyTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        [self initCollection];
    }
    return self;
}

- (void)initCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2, [UIScreen mainScreen].bounds.size.height / 2 - 80);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.height / 2 - 80)*2+30) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.bounces = NO;
    [self addSubview:_collection];
    
    [_collection release];
    //注册
    [_collection registerClass:[DailyCollectionViewCell class] forCellWithReuseIdentifier:@"DailyCollectionViewCell"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _DailyArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Daily *model = _DailyArr[indexPath.row];
    
    DailyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DailyCollectionViewCell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
    
    cell.model = model;
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _collection) {
        //取出项目的sopt_id 传到下一个页面
        Daily *model = _DailyArr[indexPath.row];
        NSDictionary *dataDic = model.data[0];
        NSString *spot_id = dataDic[@"spot_id"];
        
        ShouChangModel *shouChangModel = [[ShouChangModel alloc] init];
        [shouChangModel setValue:dataDic[@"index_title"] forKey:@"titleLabelStr"];
        [shouChangModel setValue:dataDic[@"index_cover"] forKey:@"backImageStr"];
        NSDictionary *user = dataDic[@"user"];
        
        [shouChangModel setValue:user[@"name"] forKey:@"nameLabelStr"];
        [shouChangModel setValue:user[@"avatar_l"] forKey:@"hearderImageStr"];
        
        
        NSNumber *num = dataDic[@"spot_id"];
        NSString *tripId = [NSString stringWithFormat:@"%@",num];
        
        [shouChangModel setValue:tripId forKey:@"spot_id"];
        
        [self.delegate  jumpToDailyVcWithShouChangModel:shouChangModel model:model string:spot_id];

}

}

/** 数据处理 */
- (void)handleData{
    
    if (_isWifi == NO) {
        {
            //从缓存中取
            //sdWebImage的缓存文件路径
            NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
            NSString * path1 = [path stringByAppendingPathComponent:@"Caches"];
            NSString *path2 = [path1 stringByAppendingPathComponent:@"huancun"];
            NSData *data = [NSData dataWithContentsOfFile:path2];
           
            NSDictionary *dicResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            
            
            
          
            
            NSDictionary *dataDic = dicResult[@"data"];
            
            
            NSArray *elements = dataDic[@"elements"];
            
            
            
            //初始化每日精选数组
            
            NSMutableArray *arrDaily = [NSMutableArray array];
            
            
            for (NSDictionary *dic  in elements) {
                
                if ([dic[@"desc"] isEqualToString:@""]) {
                    
                    [arrDaily addObject:dic];
                    
                }
                
            }
            //创建一个可变数组  初始化
            NSMutableArray *arrDaily1 = [NSMutableArray array];
            //取出原数组中的两个标题元素
            for (NSDictionary *dic in arrDaily) {
                
                if ([dic[@"type"] integerValue] == 10) {
                    [arrDaily1 addObject:dic];
                }
                
            }
            
            self.DailyArr = [Daily ModelHandleWithArray:arrDaily1];
            
            
            
            
            [_collection reloadData];

            
            
           
            
            
            
        }
    }

    
    
    
    
    [AFNetWorkingHandle GETRequestWithURLString:@"http://api.breadtrip.com/v2/index/" completionWithSuccess:^(id result) {
        
        
        
        
        //用字典去接受结果
        NSDictionary *dic = result;
        
        NSDictionary *dataDic = dic[@"data"];
        
        
        NSArray *elements = dataDic[@"elements"];
        
        
        
        //初始化每日精选数组
        
        NSMutableArray *arrDaily = [NSMutableArray array];
        
        
        for (NSDictionary *dic  in elements) {
            
            if ([dic[@"desc"] isEqualToString:@""]) {
                
                [arrDaily addObject:dic];
                
            }
            
        }
        //创建一个可变数组  初始化
        NSMutableArray *arrDaily1 = [NSMutableArray array];
        //取出原数组中的两个标题元素
        for (NSDictionary *dic in arrDaily) {
            
            if ([dic[@"type"] integerValue] == 10) {
                [arrDaily1 addObject:dic];
            }
            
        }
        
        self.DailyArr = [Daily ModelHandleWithArray:arrDaily1];
        
        
        
        
        [_collection reloadData];
        
    } withFailure:^(id error) {
        
    }];
    
    
}





- (void)dealloc{
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
